//
//  RateDetailCell.m
//  NiuBanJinGold
//
//  Created by Billy on 15/11/20.
//  Copyright © 2015年 zzjr. All rights reserved.
//

// 详情cell

#import "RateDetailCell.h"

@interface RateDetailCell ()

@property (nonatomic, strong) NSDictionary *dic_colorsForAllTickets; //存储颜色取值
@property (nonatomic, strong) CAGradientLayer *gradientLayer_left ; //左边部分,渲染颜色联取值based on type


@property (nonatomic, strong) CATextLayer *textLayer_rate ;//利率
@property (nonatomic, strong) CATextLayer *textLayer_period ;//期限
@property (nonatomic, strong) CATextLayer *textLayer_expiredDate ;//到期时间指示
@property (nonatomic, strong) UILabel *label_expiredDate;//到期时间指示 label 重写

@property (nonatomic, strong) CATextLayer *textLayer_colorPromt; //颜色提示

@property (nonatomic, strong) CATextLayer *textLayer_usagePrompt; //使用状态提示

@property (nonatomic, strong) UIImageView *imageView_centerPer; //百分比

@end

@implementation RateDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setBackgroundColor:BASECOLOR];
        [self subViewsCreation];
    }
    
    return self;
}

#pragma mark - subviews creation

- (void) subViewsCreation {
    
    // 实现方法:一个UIview ,利用渐变色，然后添加子view
    // 1. 顶部view
    UIView *view_top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, MULTIPLYHEIGHT(30.0/2))];
    [self.contentView addSubview:view_top];
    
    //2. 所有的内容的父view
    
    UIView *view_parent = [self viewCreationWithFrame:CGRectMake(MULTIPLYWIDTH(30.0/2), MULTIPLYHEIGHT(30.0/2), SCREEN_WIDTH - MULTIPLYWIDTH(60.0/2), MULTIPLYHEIGHT(150.0/2))];
    
    [view_parent.layer setCornerRadius:4.0];
    [view_parent.layer setMasksToBounds:YES];
    //    [view_parent setBackgroundColor:[UIColor whiteColor]];
    [self.contentView addSubview:view_parent];
    
    /** 3 . view_parent 的subviews
     *       1. left  2. center view 3. right view
     *     left and right view need to set property as its color was changed based on ticket type
     */
    
    self.gradientLayer_left = [CAGradientLayer layer];
    [self.gradientLayer_left setFrame:CGRectMake(0, 0, MULTIPLYWIDTH(193.0/2), view_parent.bounds.size.height)];
    [self.gradientLayer_left setCornerRadius:4.0];
    
    // center
    
    CALayer *layer_center = [self layerCreationWithFrame:CGRectMake(CGRectGetMaxX(self.gradientLayer_left.frame)-2.0, 0,MULTIPLYWIDTH((497.0)/2) +2.0, MULTIPLYHEIGHT(150.0/2)) backgroundColor:[UIColor whiteColor]];
    
    //顺序不能变
    [view_parent.layer addSublayer:layer_center];
    
    [view_parent.layer addSublayer:self.gradientLayer_left];
    
    //5. left layer 中间的虚线
    
    UIImageView *imageView_virtual = [[UIImageView alloc] initWithFrame:CGRectMake(0, MULTIPLYHEIGHT(96.0/2), self.gradientLayer_left.bounds.size.width, 1.0)];
    [view_parent addSubview:imageView_virtual];
    
    [imageView_virtual setImage:[UIImage imageNamed:@"virtual"]];
    
    
    // 6 年利率,期限,有效期
    
    self.textLayer_rate = [self textLayerCreationWithFrame:CGRectMake(0, 0, MULTIPLYWIDTH(193.0/2), MULTIPLYHEIGHT(96.0/2)) content:@"+1%" fontSize:35.0];
    [self.gradientLayer_left addSublayer:self.textLayer_rate];
    
    
    self.textLayer_period = [self textLayerCreationWithFrame:CGRectMake(0, CGRectGetMaxY(imageView_virtual.frame)+MULTIPLYHEIGHT(2.0), MULTIPLYWIDTH(193.0/2), MULTIPLYHEIGHT(53.0/2)) content:@"期限30天" fontSize:15.0];
    [self.gradientLayer_left addSublayer:self.textLayer_period];
    
    // 7. center view 中的内容
    
    self.imageView_centerPer = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.gradientLayer_left.frame)+MULTIPLYWIDTH(48.0/2), MULTIPLYHEIGHT(36.0/2), MULTIPLYWIDTH(28.0/2), MULTIPLYHEIGHT(28.0/2))];
    
    [self.imageView_centerPer setImage:[UIImage imageNamed:@"bluePer"]];
    [view_parent addSubview:self.imageView_centerPer];
    
    self.textLayer_colorPromt = [self textLayerCreationWithFrame:CGRectMake(CGRectGetMaxX(self.imageView_centerPer.frame) +MULTIPLYWIDTH(4.0/2), self.imageView_centerPer.frame.origin.y - MULTIPLYHEIGHT(4.0), MULTIPLYWIDTH(180.0/2), MULTIPLYHEIGHT(45.0/2)) content:@"蓝色加息券" fontSize:15.0];
    
    [view_parent.layer addSublayer:self.textLayer_colorPromt];
    [self.textLayer_colorPromt setForegroundColor:[UIColor blackColor].CGColor];
    
    // 重 用label实现
    self.label_expiredDate = [UsefulUtil labelCreationWithFrame:CGRectMake(CGRectGetMinX(self.imageView_centerPer.frame), view_parent.bounds.size.height - MULTIPLYHEIGHT(65.0/2), MULTIPLYWIDTH(262.0/2), MULTIPLYHEIGHT(30.0/2)) textColor:[UsefulUtil colorWithHexString:@"#989898"] fontName:[UIFont systemFontOfSize:12] text:@"持续时间:2015-09-20"];
    
    [view_parent addSubview:self.label_expiredDate];
}

#pragma mark - assign values

- (void)layoutSubviews {
    
    //按不同种类给ticket 渲染颜色 赋值
    NSString *left_upper; // 上半部分
    NSString *left_lower ; //下半部分
    
    if (self.rateDetail.rateType == RateTypeYellow ) {
        
        left_upper = [[self.dic_colorsForAllTickets objectForKey:@"yellowType"] firstObject];
        left_lower =  [[self.dic_colorsForAllTickets objectForKey:@"yellowType"] lastObject];
        
        [self.textLayer_colorPromt setString:@"黄色加息券"];

        [self.imageView_centerPer setImage:[UIImage imageNamed:@"ticketYellow"]];
        
    } else if (self.rateDetail.rateType == RateTypeBlue) {
        
        left_upper = [[self.dic_colorsForAllTickets objectForKey:@"blueType"] firstObject];
        left_lower =  [[self.dic_colorsForAllTickets objectForKey:@"blueType"] lastObject];
        [self.textLayer_colorPromt setString:@"蓝色加息券"];
        
        [self.imageView_centerPer setImage:[UIImage imageNamed:@"ticketBlue"]];
        
    } else if (self.rateDetail.rateType == RateTypeOrange) {
        
        left_upper = [[self.dic_colorsForAllTickets objectForKey:@"orangeType"] firstObject];
        left_lower =  [[self.dic_colorsForAllTickets objectForKey:@"orangeType"] lastObject];
        [self.textLayer_colorPromt setString:@"橙色加息券"];
        
        [self.imageView_centerPer setImage:[UIImage imageNamed:@"ticketOrange"]];
        
    }else if (self.rateDetail.rateType == RateTypeRed) {
        
        left_upper = [[self.dic_colorsForAllTickets objectForKey:@"orangeRed"] firstObject];
        left_lower =  [[self.dic_colorsForAllTickets objectForKey:@"orangeRed"] lastObject];
        [self.textLayer_colorPromt setString:@"红色加息券"];
    
        [self.imageView_centerPer setImage:[UIImage imageNamed:@"ticketRed"]];
    }
    
    [self.gradientLayer_left setLocations:[NSArray arrayWithObjects:[NSNumber numberWithFloat:(96.0/150.0)],[NSNumber numberWithFloat:(96.0/150.0)], nil]];
 
    [self.gradientLayer_left setColors:[NSArray arrayWithObjects:(id)[UsefulUtil colorWithHexString:left_upper].CGColor,
                                        (id)[UsefulUtil colorWithHexString:left_lower].CGColor,nil]];
    
    // 年利率 value ,将真实数据赋值在这
    [self.textLayer_rate setString:[NSString stringWithFormat:@"+%@%%",self.rateDetail.rate_number_value]];
    
    [self.textLayer_period setString:[NSString stringWithFormat:@"使用中"]];

    [self.label_expiredDate setText:[NSString stringWithFormat:@"持续时间:%@",self.rateDetail.rate_expiredDate]];
}

#pragma mark - setter

- (NSDictionary *)dic_colorsForAllTickets {
    
    if (!_dic_colorsForAllTickets) {
        
        _dic_colorsForAllTickets = [NSDictionary dictionaryWithObjectsAndKeys:@[@"#8fd0f1",@"#57b3e0"],@"blueType", @[@"#ffd051",@"#fda946"],@"yellowType",@[@"#ff7200",@"#ff5a00"],@"orangeType",@[@"#fb4747",@"#cc1313"],@"orangeRed",nil];
    }
    
    return _dic_colorsForAllTickets;
}

- (UIView *) viewCreationWithFrame:(CGRect ) frame {
    
    UIView *tmp = [[UIView alloc] initWithFrame:frame];
    //    [self.contentView addSubview:tmp];
    
    return tmp;
}

- (CALayer *) layerCreationWithFrame: (CGRect ) frame  backgroundColor:(UIColor *) bgColor {
    
    CALayer *tmp = [CALayer layer];
    
    [tmp setFrame:frame];
    [tmp setBackgroundColor:bgColor.CGColor];
    return tmp;
}

- (CATextLayer *) textLayerCreationWithFrame: (CGRect ) frame  content:(NSString *) text fontSize:(CGFloat) size {
    
    CATextLayer *tmp = [CATextLayer layer];
    
    [tmp setFrame:frame];
    [tmp setString:text];
    [tmp setFontSize:size];
    [tmp setAlignmentMode:kCAAlignmentCenter];
    [tmp setContentsScale:2.0];
    [tmp setWrapped:YES];
    
    return tmp;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
